/**
 * 右侧快捷操作组件，导出方法，在配置文件中指定方法即可自动引入右侧菜单
 */
define("cps_operations", ['gitee', 'utils'], function (gitee, utils) {

    // 保存chrome浏览器的书签数据到Public
    function loadChromePublicBm() {

        if(!confirm("确认保存浏览器书签到 data/bm.chrome.home.json 文件？")) return;

        var targetExtensionId = "mpgafeaocjjhghmhagcbdfddidphoilo"; // 插件的ID
        chrome.runtime.sendMessage(targetExtensionId, { type: 'getBookmarks' }, function (response) {
            gitee.updateFileCache('data/bm.chrome.home.json', JSON.stringify(response));
        });
    }

    function revertChromePublicBm() {
        if(!confirm("确认还原 data/bm.chrome.home.json 文件内容到浏览器书签？")) return;
        gitee.getFileContent('data/bm.chrome.home.json', false, false).then(data => {
            var targetExtensionId = "mpgafeaocjjhghmhagcbdfddidphoilo"; // 插件的ID
            chrome.runtime.sendMessage(targetExtensionId, { type: 'revertBookmarks', data }, function (response) {

            });
        })
    }

    function clearCache() {
        if (confirm("确认清空所有缓存？")) {
            let mdfFiles = getMdfFiles();
            if (mdfFiles.length > 0) {
                utils.confirm(`存在已修改未上传的文件，是否继续清空缓存？【${mdfFiles.map(file => file.path).join("；")}】`).then(() => {
                    gitee.clearAllCache();
                    location.reload();
                }).catch(() => {
                });
            } else {
                gitee.clearAllCache();
                location.reload();
            }

        }
    }

    function logout() {
        if (confirm("确认登出？")) {
            let mdfFiles = getMdfFiles();
            if (mdfFiles.length > 0) {
                debugger
                utils.confirm(`存在已修改未上传的文件，是否继续登出？【${mdfFiles.map(file => file.path).join("；")}】`).then(() => {
                    gitee.clearAllCache();
                    localStorage.removeItem("wyd-login-state");
                    location.reload();
                }).catch(() => {
                });
            } else {
                gitee.clearAllCache();
                localStorage.removeItem("wyd-login-state");
                location.reload();
            }

        }
    }

    async function uploadAll() {
        let mdfFiles = await getMdfFiles();
        if (mdfFiles.length > 0) {
            utils.confirm(`已修改未上传的文件，是否全部上传？【${mdfFiles.map(file => file.path).join("；")}】`).then(() => {
                updateServer(0, mdfFiles);
            })
        } else {
            utils.message("不存在变动的文件！");
        }
    }

    function updateServer(idx, mdfFiles) {
        if (idx === mdfFiles.length) {
            gitee.getFileTree(false).then(async () => {
                let editor = utils.getVueCps('editor');
                await editor.initFileTree();
                editor.$forceUpdate();
            });
            return;
        }
        let file = mdfFiles[idx];
        gitee.updateFile(file.path, file.localContent, gitee.getRepo(), false).then(() => {
            setTimeout(() => {
                updateServer(++idx, mdfFiles);
            }, 1000)
        });

    }

    async function getMdfFiles() {
        let mdfFiles = [];
        let repoFiles = await gitee.gitTable.filter(file => file.repo === gitee.getRepo()).toArray();
        for (let file of repoFiles) {
            file.hasMdf && mdfFiles.push(file);
        }
        return mdfFiles;
    }

    return {
        clearCache,
        uploadAll,
        logout,
        loadChromePublicBm,
        revertChromePublicBm
    }

});